package com.sth.edu.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.sth.commonutils.R;
import com.sth.edu.entity.Teacher;
import com.sth.edu.entity.vo.TeacherQuery;
import com.sth.edu.service.TeacherService;
import com.sth.edu.utils.UUIDUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;

/**
 * <p>
 * 讲师 前端控制器
 * </p>
 *
 * @author sth
 * @since 2020-11-29
 */
@Api(description = "讲师管理")
@RestController
@RequestMapping("/eduservice/teacher")
@CrossOrigin
public class TeacherController {
    @Autowired
    private TeacherService teacherService;

    @ApiOperation(value = "所有讲师列表")
    @GetMapping("findAll")
    public R list() {
        List<Teacher> list = teacherService.list(null);
//        try {
//            int a = 10 / 0;
//        } catch (Exception e) {
//            throw new SthException(20001, "出现自定义异常SthException");
//        }
        return R.ok().data("items", list);
    }

    @ApiOperation(value = "根据id删除讲师")
    @DeleteMapping("{id}")
    public R removeById(@ApiParam(name = "id", value = "讲师ID", required = true)
                        @PathVariable String id) {
        teacherService.removeById(id);
        return R.ok();
    }

    /**
     * 3. 分页查询讲师的方法
     *
     * @param current 当前页
     * @param limit   每页记录数
     * @return page
     */
    @ApiOperation(value = "分页条件")
    @GetMapping("pageTeacher/{current}/{limit}")
    public R pageListTeacher(@PathVariable long current,
                             @PathVariable long limit) {
        //创建page对象
        Page<Teacher> pageTeacher = new Page<>(current, limit);
        //调用分页方法实现
        // 底层会封装,把分页所有数据封装到paeTeacher 对象里面
        teacherService.page(pageTeacher, null);
        long total = pageTeacher.getTotal();
        List<Teacher> records = pageTeacher.getRecords();
        Map map = new HashMap();
        map.put("total", total);
        map.put("rows", records);
        //return R.ok().data(map);
        return R.ok().data("total", total).data("rows", records);
    }

    /**
     * 4. 分页查询带条件
     *
     * @param current 当前页
     * @param limit   每页记录数
     * @param query   TeacherQuery
     * @return R
     */
    @ApiOperation(value = "分页条件查询")
    @PostMapping("pageTeacherCondition/{current}/{limit}")
    public R pageTeacherCondition(@PathVariable long current,
                                  @PathVariable long limit, @RequestBody(required = false) TeacherQuery query) {
        // 创建page对象
        Page<Teacher> page = new Page<>(current, limit);

        // 构造条件
        QueryWrapper<Teacher> wrapper = new QueryWrapper<>();
        // 多条件组合查询 as MyBatis中的动态sql
        String name = query.getName();
        String begin = query.getBegin();
        Integer level = query.getLevel();
        String end = query.getEnd();
        // 判读条件值是否为空
        if (!StringUtils.isEmpty(name)) {
            wrapper.like("name", name);
        }
        if (!StringUtils.isEmpty(level)) {
            wrapper.eq("level", level);
        }
        if (!StringUtils.isEmpty(begin)) {
            wrapper.ge("gmt_create", begin);
        }
        if (!StringUtils.isEmpty(end)) {
            wrapper.le("gmt_create", end);
        }
        wrapper.orderByDesc("gmt_create");
        //调用方法实现分页带条件查询
        teacherService.page(page, wrapper);
        long total = page.getTotal();
        List<Teacher> records = page.getRecords();
        return R.ok().data("total", total).data("rows", records);
    }

    /**
     * 新增讲师
     *
     * @param teacher teacher
     * @return R
     */
    @ApiOperation(value = "新增讲师")
    @PostMapping("addTeacher")
    public R save(
            @ApiParam(name = "teacher", value = "讲师对象", required = true)
            @RequestBody Teacher teacher) {
        //TODO 怎么修改数据库是String 这里是long teacher.setId("1223");
        String uuid = UUIDUtil.get4UUID();
        System.out.println(uuid);
        teacher.setId(String.valueOf(UUID.randomUUID()));
        boolean save = teacherService.save(teacher);
        return save ? R.ok() : R.error();
    }

    /**
     * 根据ID查询讲师
     *
     * @param id id
     * @return
     */
    @ApiOperation(value = "根据ID查询讲师")
    @GetMapping("{id}")
    public R getById(
            @ApiParam(name = "id", value = "讲师ID", required = true)
            @PathVariable String id) {
        Teacher teacher = teacherService.getById(id);
        return R.ok().data("teacher", teacher);
    }

    @ApiOperation(value = "根据ID修改讲师")
    @PutMapping("{id}")
    public R updateById(
            @ApiParam(name = "id", value = "讲师ID", required = true)
            @PathVariable String id,

            @ApiParam(name = "teacher", value = "讲师对象", required = true)
            @RequestBody Teacher teacher) {
        teacher.setId(id);
        boolean flag = teacherService.updateById(teacher);
        return flag ? R.ok() : R.error();
    }

    @ApiOperation(value = "修改讲师传teacher对象")
    @PostMapping("updateTeacher")
    public R updateTeacher(@RequestBody Teacher teacher) {
        boolean flag = teacherService.updateById(teacher);
        return flag ? R.ok() : R.error();
    }
}

